package com.example.zqm.git72.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.zqm.git72.JavaBean.Activity;
import com.example.zqm.git72.JavaBean.Mine;
import com.example.zqm.git72.JavaBean.User;
import com.example.zqm.git72.Model.ActivityModel;
import com.example.zqm.git72.Model.UserModel;
import com.example.zqm.git72.R;
import com.example.zqm.git72.Service.ActivityService;
import com.squareup.picasso.Picasso;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class ActivityOrderActivity extends AppCompatActivity {
    private TextView jiesuan;
    private ImageView btn_back;
    private EditText name1,name2,cardid1,cardid2,tel;
    private String activityname,activityprice,activityprice1,activityintro,activity_id,payall;
    private TextView activity_name,activity_price,activity_price1,activity_intro,activity_buynum,activity_buynum1;
    private TextView uname,priceall;
    private ImageButton btn_add,btn_add1,btn_minus,btn_minus1;
    private int num,num1;
    private Double sum;
    private Double activity_Price,activity_Price1;
    private SharedPreferences sp;
    private String  user_id,paytype;
    private String user_tel,ischeck1,paynotice;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_activityorder);
        initViews();
        //initData();
         getUserTel();
        baoming();

    }
    private void initViews() {

        Intent intent = getIntent();
        activity_id = intent.getStringExtra("id");
        activity_name=(TextView)findViewById(R.id.activityname1);
        activityname = intent.getStringExtra("activityname");
        activity_name.setText(activityname);

        activity_price=(TextView)findViewById(R.id.price);
        activityprice = intent.getStringExtra("activityprice");
        activity_price.setText(activityprice);
        activity_price1=(TextView)findViewById(R.id.price_stu);
        activityprice1 = intent.getStringExtra("activityprice1");
        activity_price1.setText(activityprice1);

        activity_intro=(TextView)findViewById(R.id.need);
        activityintro = intent.getStringExtra("activityintro");
        activity_intro.setText(activityintro);
//        activity_id = intent.getStringExtra("id");
        name1=(EditText) findViewById(R.id.name1);
        name2=(EditText) findViewById(R.id.name2);
        cardid1=(EditText) findViewById(R.id.cardid1);
        cardid2=(EditText) findViewById(R.id.cardid2);
        tel=(EditText)findViewById(R.id.tel);
        jiesuan=(TextView)findViewById(R.id.jiesuan);
        uname=(TextView)findViewById(R.id.participantname);
//         Bundle bundle = getIntent().getExtras();
//        activity_Price = bundle.getDouble("activity_price");
//        activity_Price1 = bundle.getDouble("activity_price1");
//        btn_add = (ImageButton)findViewById(R.id.btn_add);
//        btn_minus = (ImageButton)findViewById(R.id.btn_minus);
//        btn_add1 = (ImageButton)findViewById(R.id.btn_add1);
//        btn_minus1 = (ImageButton)findViewById(R.id.btn_minus1);
          priceall=(TextView)findViewById(R.id.priceAll);
          payall=intent.getStringExtra("activityprice");
          priceall.setText(payall);
//        activity_buynum=(TextView)findViewById(R.id.tv_buy_num);
//        activity_buynum1=(TextView)findViewById(R.id.tv_buy_num1);

    }
//    private void initData() {
//        getUserTel();
//        activity_name.setText(activityname);
//        activity_price.setText(activityprice);
//        activity_price1.setText(activityprice1);
//        activity_intro.setText(activityintro);
//        activity_tel.setText(user_tel);
//        activity_buynum.setText("");
//        activity_buynum1.setText("");
//        String fnum = activity_buynum.getText().toString();
//        String fnum1 = activity_buynum1.getText().toString();
//        num = Integer.parseInt(fnum);
//        num1 = Integer.parseInt(fnum1);
//        Double numb = Double.parseDouble(fnum);
//        Double numb1 = Double.parseDouble(fnum1);
//        sum = (numb*activity_Price)+(numb1*activity_Price1);
//        activity_priceall.setText(sum.toString());
//
//        btn_add.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
////                Toast.makeText(BuyActivity.this,"add",Toast.LENGTH_SHORT).show();
//                if (num>=0){
//                    num++;
//                }
//                activity_buynum.setText(Integer.toString(num));
//                String fnum = activity_buynum.getText().toString();
//                String fnum1 = activity_buynum1.getText().toString();
//                num = Integer.parseInt(fnum);
//                num1 = Integer.parseInt(fnum1);
//                Double numb = Double.parseDouble(fnum);
//                Double numb1 = Double.parseDouble(fnum1);
//                sum = (numb*activity_Price)+(numb1*activity_Price1);
//                activity_priceall.setText(sum.toString());
//            }
//        });
//        btn_minus.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                if (num>0){
//                    num--;
//                }else{
//                    Toast.makeText(ActivityOrderActivity.this,"购买数量不能低于0！",Toast.LENGTH_SHORT).show();
//                }
//                activity_buynum.setText(Integer.toString(num));
//                String fnum = activity_buynum.getText().toString();
//                String fnum1 = activity_buynum1.getText().toString();
//                num = Integer.parseInt(fnum);
//                num1 = Integer.parseInt(fnum1);
//                Double numb = Double.parseDouble(fnum);
//                Double numb1 = Double.parseDouble(fnum1);
//                sum = (numb*activity_Price)+(numb1*activity_Price1);
//                activity_priceall.setText(sum.toString());
//            }
//        });
//        btn_add1.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
////                Toast.makeText(BuyActivity.this,"add",Toast.LENGTH_SHORT).show();
//                if (num1>=0){
//                    num1++;
//                }
//                activity_buynum1.setText(Integer.toString(num1));
//                String fnum = activity_buynum.getText().toString();
//                String fnum1 = activity_buynum1.getText().toString();
//                num = Integer.parseInt(fnum);
//                num1 = Integer.parseInt(fnum1);
//                Double numb = Double.parseDouble(fnum);
//                Double numb1 = Double.parseDouble(fnum1);
//                sum = (numb*activity_Price)+(numb1*activity_Price1);
//                activity_priceall.setText(sum.toString());
//            }
//        });
//        btn_minus1.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                if (num1>0){
//                    num1--;
//                }else{
//                    Toast.makeText(ActivityOrderActivity.this,"购买数量不能低于0！",Toast.LENGTH_SHORT).show();
//                }
//                activity_buynum1.setText(Integer.toString(num1));
//                String fnum = activity_buynum.getText().toString();
//                String fnum1 = activity_buynum1.getText().toString();
//                num = Integer.parseInt(fnum);
//                num1 = Integer.parseInt(fnum1);
//                Double numb = Double.parseDouble(fnum);
//                Double numb1 = Double.parseDouble(fnum1);
//                sum = (numb*activity_Price)+(numb1*activity_Price1);
//                activity_priceall.setText(sum.toString());
//            }
//        });
//        jiesuan.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                Intent intent=new Intent(ActivityOrderActivity.this,ActivityOrderActivity.class);
//                String activityname = activity_name.getText().toString();
//                String activitynum = activity_buynum.getText().toString();
//                String activitynum1 = activity_buynum1.getText().toString();
//                String activitypriceall = sum.toString();
//                String activitytel = activity_tel.getText().toString();
//                intent.putExtra("activityname",activityname);
//                intent.putExtra("activitynum",activitynum);
//                intent.putExtra("activitynum1",activitynum1);
//                intent.putExtra("activitypriceall",activitypriceall);
//                intent.putExtra("user_tel",activitytel);
////                isCheck();
////                intent.putExtra("ischeck",ischeck1);
//                setResult(RESULT_OK,intent);
//                baoming();
//                startActivity(intent);
//            }
//        });
//    }
        public void getUserTel(){
            SharedPreferences sharedPreferences=getSharedPreferences("userInfo", Context.MODE_PRIVATE);
            user_id=sharedPreferences.getString("user_id","");
            UserModel userModel =new UserModel();
            Call<Mine> call =userModel.getUser(user_id);
            call.enqueue(new Callback<Mine>() {
            @Override
            public void onResponse(Call<Mine> call, Response<Mine> response) {
                  Mine userinfo=response.body();
                uname.setText(userinfo.getUser_name());
            }

            @Override
            public void onFailure(Call<Mine> call, Throwable t) {

            }
        });
    }
    public  void baoming(){
        jiesuan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String participantname = uname.getText().toString();
//        String activitynum = activity_buynum.getText().toString();
//        String activitynum1 = activity_buynum1.getText().toString();
//        String activitypriceall = sum.toString();
                String participanttel = tel.getText().toString();
                String act_id=activity_id;
                String userid=user_id;
                String pay=payall;
                String insurance = name1.getText().toString()+"+"+cardid1.getText().toString()+","+name2.getText().toString()+"+"+cardid2.getText().toString();
                ActivityModel activityModel =new ActivityModel();
                Call<Integer> call =activityModel.signActivity(participantname,act_id,participanttel,pay,insurance,userid);
                call.enqueue(new Callback<Integer>() {
                    @Override
                    public void onResponse(Call<Integer> call, Response<Integer> response) {
                            Toast.makeText(ActivityOrderActivity.this,"报名成功",Toast.LENGTH_SHORT).show();

                    }

                    @Override
                    public void onFailure(Call<Integer> call, Throwable t) {

                    }
                });

            }
        });

    }
}
